package com.ahao.sdk.ui.base;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.ahao.platform.R;
import com.ahao.sdk.utils.LogUtils;

public abstract class BaseFullScreenWebViewDialog extends Dialog {

    private JSBridge jsBridge ;
    protected WebView webView;
    private ViewGroup contentView;

    public BaseFullScreenWebViewDialog(Context context) {
        super(context);
        firstSetting();

    }

    public BaseFullScreenWebViewDialog(Context context, int themeResId) {
        super(context, themeResId);
        firstSetting();

    }

    protected BaseFullScreenWebViewDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        firstSetting();
    }

    private void firstSetting() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().getAttributes().dimAmount = 0.01f;
        //打开硬件加速
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }

    @SuppressLint("DiscouragedApi")
    private void init(Context context) {
        jsBridge = new JSBridge(this);
        LogUtils.info("init webview Dialog");
        webView = findViewById(R.id.ahao_webview);
        initWebViewSettings(webView);
        addJSInterface(webView);
        webView.setBackgroundColor(0);
        webView.getBackground().setAlpha(0);
        setCancelable(false);
        initView(contentView);
    }

    protected abstract void initView(ViewGroup contentView);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       contentView = (ViewGroup) getLayoutInflater().inflate(R.layout.ahao_layout_webview,null);
       setContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
       init(getContext());
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebViewSettings(WebView webView) {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);  // 允许 JavaScript
        webSettings.setDomStorageEnabled(true);  // 允许使用 DOM Storage API
        webSettings.setDatabaseEnabled(true);    // 允许使用数据库
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);  // 允许加载混合内容（http 和 https 混合）

        // 添加其他 WebView 设置...

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                // 处理新窗口中的链接点击事件
                try {
                    String url = request.getUrl().toString();
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    view.getContext().startActivity(intent);
                }catch (Exception e){
                    e.printStackTrace();
                }
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                Log.d("WebViewConsole", consoleMessage.message() + " -- From line "
                        + consoleMessage.lineNumber() + " of "
                        + consoleMessage.sourceId());
                return true;
            }
        });
        // webview允许打开外部浏览器

    }
    private void addJSInterface(WebView webView) {
        if (jsBridge != null) {
            webView.addJavascriptInterface(jsBridge, "JSBridge");
        }
    }


}
